package bot.pojo;

import bot.conf.MpperUnit;
import bot.mapper.MiUserTableMapper;
import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.*;

//定时器，循环查询二维码是否被扫描
public class TimerExample {
    private static MiUserTableExample miUserTableExample = new MiUserTableExample();
    public static Map<String , String > stati = new HashMap<>();//每个群下同一时间只能创建一个二维码
    public static String Start(String qqId , String tick){
        String s = stati.get(qqId);
        if(s != null && s.equals("true")){//表示当前群有还未失效的二维码
            return "err";
        }
        MiUserTableMapper miUserTableMapper = MpperUnit.staticmpperUnit.getMiUserTableMapper();

        Timer timer = new Timer();
        int delay = 3000; // 延迟时间，单位为毫秒
        int period = 3000; // 执行间隔时间，单位为毫秒
        int duration = 60; // 持续时间，单位为秒
        TimerTask task = new TimerTask() {
            int count = 0;
            @Override
            public void run() {
                // 需要执行的代码块
                //如果一分钟内无扫码，或扫了并且确认登录后取消定时任务
                Map head = new HashMap<>();
                head.put("x-rpc-app_id","bll8iq97cem8");//必须携带这俩请求头，否则无法访问
                head.put("x-rpc-device_id","bll8iq97cem8");

                QrlogStatic qrlogStatic = new QrlogStatic();
                qrlogStatic.setTicket(tick);
                String QrStaticStr = bot.controller.HttpHelp.sendPost("passport-api.miyoushe.com/account/ma-cn-passport/web/queryQRLoginStatus",JSONObject.toJSONString(qrlogStatic),head);
                //如果二维码被扫描且确认则去掉这个定时任务
                MiUserInfo miUserInfo = JSONObject.parseObject(QrStaticStr, MiUserInfo.class);
                if(miUserInfo.getData().getStatus().equals("Confirmed")){
                    System.out.println("扫码完成");
                    //记录用户数据
                    miUserTableExample.clear();
                    miUserTableExample.createCriteria().andMidEqualTo(miUserInfo.getData().getUser_info().getMid());
                    List<MiUserTable> miUserTables = miUserTableMapper.selectByExample(miUserTableExample);

                    MiUserTable miUserTable = new MiUserTable();
                    miUserTable.setAppId(miUserInfo.getData().getApp_id());
                    miUserTable.setAid(miUserInfo.getData().getUser_info().getAid());
                    miUserTable.setMid(miUserInfo.getData().getUser_info().getMid());
                    List<Link> links = miUserInfo.getData().getUser_info().getLinks();
                    System.out.println(JSONObject.toJSONString(links));
                    miUserTable.setLinkJsonstr(JSONObject.toJSONString(links));
                    miUserTable.setMobile(miUserInfo.getData().getUser_info().getMobile());
                    miUserTable.setQqId(qqId);
                    if(miUserTables.size() == 0){//若有重复数据只更新
                        miUserTable.setCreateTime(new SimpleDateFormat("YYYY-MM-dd HH:mm").format(new Date()));
                        miUserTableMapper.insert(miUserTable);
                    }else {
                        miUserTable.setId(miUserTables.get(0).getId());
                        miUserTableMapper.updateByPrimaryKeySelective(miUserTable);
                    }
                    stati.remove(qqId);
                    cancel();
                }
                count++;
                if (count >= duration / (period / 1000)) {
                    //过期清除当前群二维码有效状态
                    stati.remove(qqId);
                    // 达到指定的持续时间后取消定时任务
                    cancel();
                    System.out.println("定时器任务结束");
                }
            }
        };
        // 使用定时器调度定时任务
        timer.schedule(task, delay, period);
        return "ok";
    }
}
